package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.liwalo.entities.ContactoTienda;
import com.liwalo.entities.Tienda;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class ContactoTienda.
 * @see com.liwalo.dao.ContactoTienda
 * @author Hibernate Tools
 */
public class ContactoTiendaDAO {

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public void persist(ContactoTienda transientInstance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(transientInstance);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			re.printStackTrace();
		}
	}

	public void update(ContactoTienda instance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.saveOrUpdate(instance);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		}
	}

	public void attachClean(ContactoTienda instance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.lock(instance, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			tx.rollback();
			throw re;
		}
	}

	public void delete(ContactoTienda persistentInstance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			session.delete(persistentInstance);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public ContactoTienda merge(ContactoTienda detachedInstance) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			ContactoTienda result = (ContactoTienda) session.merge(detachedInstance);
			tx.commit();
			return result;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public ContactoTienda findById(java.lang.Integer id) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		try {
			ContactoTienda instance = (ContactoTienda) session.get(ContactoTienda.class,id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public ContactoTienda findByIdTiena(Tienda tiendavo) {
		List result = null;
		Transaction tx = null;
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		try{
            
            tx = session.beginTransaction();

            Criteria criteria = session.createCriteria(ContactoTienda.class, "contactotienda")
            //.createCriteria("contactotienda.tiendaId" , "tiendaid")
            .add(Restrictions.eq("contactotienda.tiendaId",tiendavo));
            result = criteria.list();
            tx.commit();


            if(result != null && result.size()>0){
                return (ContactoTienda)result.get(0);
            }



		} catch (RuntimeException re) {
			throw re;
		}


            return null;
  }


	@SuppressWarnings("unchecked")
	public List<ContactoTienda> findByExample(ContactoTienda instance) {
		Session session = sessionFactory.getCurrentSession();
		try {
			session.beginTransaction();
			Criteria crit = session.createCriteria("com.liwalo.dao.ContactoTienda").add(create(instance));
			List<ContactoTienda> results = (List<ContactoTienda>) crit.list();			
			return results;
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
